package com.base.system.managedBean;

import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;

import javax.faces.context.FacesContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.operamasks.faces.annotation.Action;
import org.operamasks.faces.annotation.BeforeRender;
import org.operamasks.faces.annotation.Bind;
import org.operamasks.faces.annotation.DataModel;
import org.operamasks.faces.annotation.Inject;
import org.operamasks.faces.annotation.ManagedBean;
import org.operamasks.faces.annotation.ManagedBeanScope;
import org.operamasks.faces.annotation.SaveState;
import org.operamasks.faces.component.grid.CheckboxSelectionModel;
import org.operamasks.faces.component.grid.GridColumnModel;
import org.operamasks.faces.component.grid.GridHeaderModel;
import org.operamasks.faces.component.grid.GridSelectionModel;
import org.operamasks.faces.component.grid.impl.UIDataGrid;
import org.operamasks.faces.component.grid.provider.GridViewProvider;
import org.operamasks.faces.user.util.Browser;

import com.base.commons.SpringBeanHolder;
import com.base.organization.entity.DepartmentAndPerson;
import com.base.organization.service.IDepartmentAndPersonDAO;
import com.base.organization.service.IOrganizationService;

/**
 * 
 * This managed bean is generated automatically
 */
@ManagedBean(name = "system_departmentListOfOnePersonBean", scope = ManagedBeanScope.SESSION)
public class DepartmentListOfOnePersonBean implements Serializable {
	private final static Log logger = LogFactory
			.getLog(DepartmentListOfOnePersonBean.class);
	private IOrganizationService organizationService = SpringBeanHolder
			.getBean("orgService");
	private IDepartmentAndPersonDAO departmentAndPersonDAO = SpringBeanHolder
			.getBean("departmentAndPersonDAO");
	@Inject
	private FacesContext context;

	@Bind(id = "securityDepartmentGrid", attribute = "binding")
	private UIDataGrid securityDepartmentGrid;
	@SaveState
	private String userId;
	@DataModel(id = "securityDepartmentGrid")
	private List<DepartmentAndPerson> data = new LinkedList<DepartmentAndPerson>();

	@Bind(id = "securityDepartmentGrid", attribute = "viewProvider")
	private GridViewProvider viewProvider = new GridViewProvider() {

		public GridSelectionModel getSelectionModel(Object input) {
			return new CheckboxSelectionModel();
		}

		@Override
		public GridColumnModel getColumnModel(Object arg0) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public GridHeaderModel getHeaderModel(Object arg0) {
			// TODO Auto-generated method stub
			return null;
		}
	};

	@BeforeRender
	public void beforeRender(boolean isPostback) {
		if (!isPostback) {
			userId = context.getExternalContext().getRequestParameterMap().get(
					"userId");
		}
		data = organizationService.getDepAndPersonOfPerson(Integer
				.parseInt(userId), null);
	}

	@Action(id = "createDept")
	public void createDept() {
		String url = context.getExternalContext().getRequestContextPath()
				+ "/modules/system/editDepAndPerson.faces?userId="
				+ userId;
		String script = "changeSrc('" + url + "','分配所属部门')";
		Browser.execClientScript(script);
	}

	@Action(id = "editDept")
	public void editDept() {
		if (securityDepartmentGrid.getSelectedValues().length > 0) {
			DepartmentAndPerson dap = (DepartmentAndPerson) securityDepartmentGrid
					.getSelectedValues()[0];
			String url = context.getExternalContext().getRequestContextPath()
					+ "/modules/system/editDepAndPerson.faces?userId="
					+ userId
					+ "&depId="
					+ dap.getDepartmentalUnit().getDepartmentalUnitId()
							.toString();
			String script = "changeSrc('" + url + "','修改所属部门')";
			Browser.execClientScript(script);
		}
	}

	@Action(id = "deleteDept")
	public void deleteDept() {
		if (securityDepartmentGrid.getSelectedValues().length > 0) {
			DepartmentAndPerson dap = (DepartmentAndPerson) securityDepartmentGrid
					.getSelectedValues()[0];
			departmentAndPersonDAO.delete(dap);
			data.remove(dap);
			securityDepartmentGrid.reload();
		}
	}
}
